const utilService = require('../../../../utils/utilService.js');
const babyService = require('../../getBaby/getBabyService.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startPage: 0,
    items: [],
    index: 0,
    shareMsg: "",
    startPage: 0,
    hasNextPage: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // let data = JSON.parse(options.data);
    // this.setData({
    //   items: data.items
    // })
    this.loadData();
    this.getShareMessage(1)
  },


  //获取分享信息
  getShareMessage: function(type) {
    var self = this;
    utilService.shareSettings(type,
      function(data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function(error) {

      }
    );
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    var title = this.data.shareMsg
    var path = "pages/home/home?userId=" + getApp().globalData.userId
    return {
      title: title,
      path: path,

      success: function(res) {},
      fail: function(res) {}
    }
  },

  itemClick: function(e) {
    let pages = getCurrentPages();
    let that = this;
    let index = e.currentTarget.dataset.idx;
    if (pages[pages.length - 2].route == 'mineSubs/pages/getFlowers/uploadVideo/uploadVideo') {
      console.log(that.data.items, pages[pages.length - 2].data.execData)
      pages[pages.length - 2].setData({
        index: index,
        good: that.data.execData.items[index],
        order: that.data.execData.items[index]
      })
      wx.navigateBack({
        delta: 1,
      })
    }
  },

  /**
   * 加载数据
   */
  loadData: function(e) {
    let that = this;
    if (that.data.hasNextPage) {
      let param = new Object();
      param['userId'] = getApp().globalData.userId;
      param['status'] = 3;
      param['startPage'] = that.data.startPage;
      babyService.requestProducts(param, function(data) {
        if ((!data.items || data.items.length == 0) && that.data.order == null) {
          wx.showModal({
            title: '温馨提示',
            content: '没有需要感谢的宝贝',
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack();
              }
            }
          });
          return;
        }
        that.setData({
          execData: data,
          items: that.data.items.concat(data.items),
          hasNextPage: data.hasNextPage,
          startPage: that.data.startPage + 1
        });
      });
    }
  },

  /**
   * 上拉加载
   */
  onReachBottom: function(e) {
    this.loadData();
  }

})